using ImageCSharp.Core.Models;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace ImageCSharp.Controls
{
    /// <summary>
    /// 属性面板控件
    /// </summary>
    public partial class PropertiesPanel : UserControl, INotifyPropertyChanged
    {
        #region Dependency Properties

        public static readonly DependencyProperty SelectedToolProperty =
            DependencyProperty.Register("SelectedTool", typeof(Tool), typeof(PropertiesPanel),
                new PropertyMetadata(null, OnSelectedToolChanged));

        public Tool? SelectedTool
        {
            get => (Tool?)GetValue(SelectedToolProperty);
            set => SetValue(SelectedToolProperty, value);
        }

        #endregion

        #region Properties

        public string ImageSize => "800 x 600";
        public string ImageFormat => "PNG";
        public string FileSize => "2.3 MB";
        public SolidColorBrush MouseColor => Brushes.Black;
        public string MouseRGB => "RGB(0, 0, 0)";
        public string MouseHex => "#000000";

        #endregion

        public PropertiesPanel()
        {
            InitializeComponent();
            DataContext = this;
        }

        private static void OnSelectedToolChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is PropertiesPanel panel)
            {
                panel.UpdateToolProperties();
            }
        }

        private void UpdateToolProperties()
        {
            // 隐藏所有属性面板
            SelectionProperties.Visibility = System.Windows.Visibility.Collapsed;
            DrawingProperties.Visibility = System.Windows.Visibility.Collapsed;
            MeasurementProperties.Visibility = System.Windows.Visibility.Collapsed;

            if (SelectedTool == null) return;

            switch (SelectedTool.Type)
            {
                case ToolType.Selection:
                    SelectionProperties.Visibility = System.Windows.Visibility.Visible;
                    break;

                case ToolType.Drawing:
                    DrawingProperties.Visibility = System.Windows.Visibility.Visible;
                    break;

                case ToolType.Measurement:
                    MeasurementProperties.Visibility = System.Windows.Visibility.Visible;
                    break;
            }

            // 强制更新所有绑定
            SelectionProperties.DataContext = SelectedTool;
            DrawingProperties.DataContext = SelectedTool;
            MeasurementProperties.DataContext = SelectedTool;
            
            // 使用Dispatcher确保UI线程更新
            Dispatcher.Invoke(() =>
            {
                OnPropertyChanged(nameof(SelectedTool));
            });
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}